var express = require('express');
var router = express.Router();
var moment = require("moment")

var {XiaoqusModel} = require("../utils/model")

var moment = require('moment')
/* GET home page. */
router.get('/', function (req, res, next) {
  console.log(req.session)
  res.render('index', {
    title: 'Express',
    username:111,

  });
});
router.get("/village",(req,res)=>{
  var body = req.body;
  console.log(body);
 
  XiaoqusModel.find({},{})
  .then(result=>{
    console.log(result);
     res.render("village",{
       username:111,
        result
  })
  })
})

router.post("/village/abc",(req,res)=>{
  var body = req.body;
  console.log(body);
  // res.render("village",{
  //   username:1111
  // })
  console.log(XiaoqusModel);
 XiaoqusModel.insertMany(body)
 .then(result=>{
   console.log(result);
      res.redirect("/village")
 }).catch(err=>{
   console.log(err);
 })

})


//删除

router.post("/delonecomment",(req,res)=>{
  
      const {id} = req.body;
      XiaoqusModel.remove({
          id
      }).then(result=>{
          res.json({
              code:200,
              result,
              type:1,
              msg:"删除成功"
          })
      })
  
})


// 用户提交留言信息处
router.post('/msg/submit',function(req,res){
  var {mid} = req.query;
  var body = req.body;
  var username = "222";
  // res.send('评论成功')
  checkSession(req,res,username=>{
    MoviesModel.findOne({id:mid})
    .then(residents=>{
        body.time = moment().format('YYYY-MM-DD hh:mm:ss');
        body.title = title;
        body.content = content;
        body.author = username;
        body.mid = mid;
        body.residents = residents;
        // console.log(body);
        ContentModel.insertMany(body)
        .then(result=>{
            // res.send('评论成功...')
            // result.time=
            result = result.map(item=>{
                item = {...item._doc,time:moment(item.time).format('YYYY-MM-DD hh:mm:ss')}

                return item;
            })
            res.redirect("/msg")
        })
    })
})
})
// 用户留言处
router.get('/msg',function(req,res){
  var username = req.query.username;
  console.log(username);
  res.render('msg',{
    title:'留言',
    username:"123",
  })
})
// 用户留言表 处理确认
router.get('/mlist',function(req,res){
  var username = req.query.username;
  // console.log(username);
  // res.send('qifei ')
  // CommentModel.find({id},{})
  // .then(result=>{

    res.render('mlist',{
      title:'用户留言',
      username:"123",
      // result
    })
  // })
})
module.exports = router;


//注销
router.get('/logout',(req,res)=>{
  req.session.destroy(()=>{
     res.redirect("/")
  })
})